/**
 * Copyright &copy; 2016 splat  All rights reserved.
 */
package com.osig.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

import com.common.util.ConfigUtil;
import com.google.common.collect.Lists;
import com.user.entity.emp.EmpVO;
import com.osig.config.Global;
import com.osig.sys.security.SystemAuthorizingRealm.Principal;
import com.osig.util.CacheUtils;
import com.rbac.entity.RbacResources;
import com.rbac.service.PrivilegeService;



/**
 * 用户工具类
 * @author splat
 * @version 2013-12-05
 */
public class UserUtils {

	public static final String EmpVO_CACHE = "EmpVOCache";
	public static final String EmpVO_CACHE_ID_ = "id_";
	public static final String EmpVO_CACHE_LOGIN_NAME_ = "ln";
	public static final String EmpVO_CACHE_LIST_BY_OFFICE_ID_ = "oid_";

	public static final String CACHE_ROLE_LIST = "roleList";
	public static final String CACHE_ROLE_LIST_COMPANY = "roleListCompany";
	public static final String CACHE_MENU_LIST = "menuList";
	public static final String CACHE_MENU_LIST_PLATTYPE_B = "B";//采购商菜单树缓存
	public static final String CACHE_MENU_LIST_PLATTYPE_S = "S";//供应商菜单树缓存
	public static final String CACHE_MENU_LIST_PLATTYPE_A = "A";//平台菜单树缓存
	public static final String CACHE_AREA_LIST = "areaList";
	public static final String CACHE_OFFICE_LIST = "officeList";
	public static final String CACHE_OFFICE_ALL_LIST = "officeAllList";
	/*@Autowired
	private static PrivilegeService privilegeService = SpringContextHolder.getBean(PrivilegeService.class);*/

	/**
	 * 清除当前用户缓存
	 */
	public static void clearCache(){
		removeCache(CACHE_ROLE_LIST);
		removeCache(CACHE_ROLE_LIST_COMPANY);
		removeCache(CACHE_MENU_LIST);
		removeCache(CACHE_AREA_LIST);
		removeCache(CACHE_OFFICE_LIST);
		removeCache(CACHE_OFFICE_ALL_LIST);
		removeCache(CACHE_MENU_LIST_PLATTYPE_B);
		removeCache(CACHE_MENU_LIST_PLATTYPE_S);
		removeCache(CACHE_MENU_LIST_PLATTYPE_A);
		UserUtils.clearCache(getEmpVO());
	}
	
	/**
	 * 清除指定用户缓存
	 * @param EmpVO
	 */
	public static void clearCache(EmpVO empVO){
		CacheUtils.remove(EmpVO_CACHE, EmpVO_CACHE_ID_ + empVO.getCode());
	}
	
	/**
	 * 获取当前用户
	 * @return 取不到返回 new EmpVO()
	 */
	public static EmpVO getEmpVO(){
		Principal principal = getPrincipal();
		if (principal!=null){
			EmpVO empVO = principal.getEmpV0();
			if (empVO != null){
				return empVO;
			}
			return new EmpVO();
		}
		// 如果没有登录，则返回实例化空的EmpVO对象。
		return new EmpVO();
	}

	
	/**
	 * 获取当前用户授权菜单，菜单栏显示
	 * @return
	 */
	public static List<RbacResources> getMenuList(){
		System.out.println(Integer.parseInt(Global.getConfig("rbac.system")));
		System.out.println(UserUtils.getUser().getCode());
		//List<RbacResources>	resourceslist =privilegeService.getResourcesByUserId(Integer.parseInt(ConfigUtil.getValue("rbac.system")), UserUtils.getUser().getCode());
		return Lists.newArrayList();//resourceslist;
	}

	/**
	 * 获取系统主页
	 * @return
	 */
	public static String getmainUrl(){
	    String url = Global.getConfig("sso.web.site");
	    return url;
	}
	
	/**
	 * 获取当前用户授权菜单admin根据平台获取菜单,菜单管理
	 * @return
	 */
	/*public static List<Menu> getMenuList(String platType){
		@SuppressWarnings("unchecked")
		List<Menu> menuList = null;//(List<Menu>)getCache(platType);
		if (menuList == null){
			EmpVO EmpVO = getEmpVO();
			Menu m = new Menu();
			//A平台过来去掉权限验证
			if (EmpVO.isAdmin()){
				m.setPlatType(platType);
				menuList = menuDao.findAllList(m);
			}else{
				m.setEmpVOId(EmpVO.getId());
				menuList = menuDao.findByEmpVOId(m);
			}
			//menuList = menuDao.findAllList(new Menu());
			putCache(platType, menuList);
		}
		return menuList;
	}
	*/
	
	/**
	 * 获取当前用户授权的区域
	 * @return
	 */
	/*public static List<Area> getAreaList(){
		@SuppressWarnings("unchecked")
		List<Area> areaList = null;//(List<Area>)getCache(CACHE_AREA_LIST);
		if (areaList == null){
			areaList = areaDao.findAllList(new Area());
			putCache(CACHE_AREA_LIST, areaList);
		}
		return areaList;
	}*/
	
	/**
	 * 获取当前用户有权限访问的部门
	 * @return
	 */
	/*public static List<Office> getOfficeList(){
		@SuppressWarnings("unchecked")
		List<Office> officeList = null;//(List<Office>)getCache(CACHE_OFFICE_LIST);
		if (officeList == null){
			EmpVO EmpVO = getEmpVO();
			if (EmpVO.isAdmin()){//为管理员时查询所有
				officeList = officeDao.findAllList(new Office());
			}else{
				Office office = new Office();
				office.setParent(EmpVO.getCompany());//设置当前用户能拿到的机构权限
				//office.getSqlMap().put("dsf", BaseService.dataScopeFilter(EmpVO, "a", ""));//设置数据范围sql
				officeList = officeDao.findList(office);
			}
			putCache(CACHE_OFFICE_LIST, officeList);
		}
		return officeList;
	}*/

	/**
	 * 获取所有机构
	 * @return
	 */
	/*public static List<Office> getOfficeAllList(){
		@SuppressWarnings("unchecked")
		List<Office> officeList = null;//(List<Office>)getCache(CACHE_OFFICE_ALL_LIST);
		if (officeList == null){
			officeList = officeDao.findAllList(new Office());
			putCache("CACHE_OFFICE_ALL_LIST",officeList);
		}
		return officeList;
	}*/
	
	
	public static EmpVO getUser(){
		return new EmpVO();//getPrincipal().getEmpV0();
	}
	
	/**
	 * 获取授权主要对象
	 */
	public static Subject getSubject(){
		return SecurityUtils.getSubject();
	}
	
	/**
	 * 获取当前登录者对象
	 */
	public static Principal getPrincipal(){
		try{
			Subject subject = SecurityUtils.getSubject();
			Principal principal = (Principal)subject.getPrincipal();
			if (principal != null){
				return principal;
			}
//			subject.logout();
		}catch (UnavailableSecurityManagerException e) {
			
		}catch (InvalidSessionException e){
			
		}
		return null;
	}
	
	public static Session getSession(){
		try{
			Subject subject = SecurityUtils.getSubject();
			Session session = subject.getSession(false);
			if (session == null){
				session = subject.getSession();
			}
			if (session != null){
				return session;
			}
//			subject.logout();
		}catch (InvalidSessionException e){
			
		}
		return null;
	}
	
	// ============== EmpVO Cache ==============
	
	public static Object getCache(String key) {
		return getCache(key, null);
	}
	
	public static Object getCache(String key, Object defaultValue) {
//		Object obj = getCacheMap().get(key);
		Object obj = getSession().getAttribute(key);
		return obj==null?defaultValue:obj;
	}

	public static void putCache(String key, Object value) {
//		getCacheMap().put(key, value);
		getSession().setAttribute(key, value);
	}

	public static void removeCache(String key) {
//		getCacheMap().remove(key);
		getSession().removeAttribute(key);
	}

	
//	public static Map<String, Object> getCacheMap(){
//		Principal principal = getPrincipal();
//		if(principal!=null){
//			return principal.getCacheMap();
//		}
//		return new HashMap<String, Object>();
//	}
	
}
